package com.xywh.composeLibrary.view

import android.graphics.Bitmap
import android.graphics.Canvas
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.core.content.res.ResourcesCompat

//import coil.compose.AsyncImage

/**
 * 加载图片
 * @param id Int 图片的id
 * @param size Dp 图片的大小
 * @param start Dp 左边距
 * @param top Dp 上边距
 * @param end Dp 右边距
 * @param bottom Dp 底边距
 * @param roundedCornerShape Dp 圆角的大小
 * @param contentScale ContentScale 裁剪模式
 * @param contentDescription String 描述
 */
@Composable
fun XxImage(
    id: Int,
    size: Dp = 100.dp,
    start: Dp = 0.dp,
    top: Dp = 0.dp,
    end: Dp = 0.dp,
    bottom: Dp = 0.dp,
    borderWidth: Dp = 0.dp,
    borderColor: Color = Color.Transparent,
    borderShape: Shape = RectangleShape,
    roundedCornerShape: Int = 0,
    contentScale: ContentScale = ContentScale.Crop,
    contentDescription: String = "",
    colorFilter: ColorFilter? = null

) {
    Image(
        painter = painterResource(id = id),
        contentDescription = contentDescription,
        contentScale = contentScale,
        modifier = Modifier
            .padding(
                start = start,
                top = top,
                end = end,
                bottom = bottom,
            )
            .border(width = borderWidth, color = borderColor, borderShape)
            .size(size)
            .clip(RoundedCornerShape(roundedCornerShape)),
        colorFilter = colorFilter
    )

}

@Composable
fun XxImageVector(
    imageVectorId: Int,
    size: Dp = 100.dp,
    start: Dp = 0.dp,
    top: Dp = 0.dp,
    end: Dp = 0.dp,
    bottom: Dp = 0.dp,
    borderWidth: Dp = 0.dp,
    borderColor: Color = Color.Transparent,
    borderShape: Shape = RectangleShape,
    roundedCornerShape: Int = 0,
    contentScale: ContentScale = ContentScale.Crop,
    contentDescription: String = "",
    colorFilter: ColorFilter? = null,
    clickable: Boolean = true,
    onClick: () -> Unit = {}
) {
    Image(
        imageVector = ImageVector.vectorResource(id = imageVectorId),
        contentDescription = contentDescription,
        contentScale = contentScale,
        modifier = Modifier
            .padding(
                start = start,
                top = top,
                end = end,
                bottom = bottom,
            )
            .border(width = borderWidth, color = borderColor, borderShape)
            .size(size)
            .clip(RoundedCornerShape(roundedCornerShape))
            .clickable(clickable) {
                onClick.invoke()
            },
        colorFilter = colorFilter
    )

}

@Composable
fun XxImageVector(
    imageVectorId: Int,
    size: Dp = 100.dp,
    start: Dp = 0.dp,
    top: Dp = 0.dp,
    end: Dp = 0.dp,
    bottom: Dp = 0.dp,
    borderWidth: Dp = 0.dp,
    borderColor: Color = Color.Transparent,
    borderShape: Shape = RectangleShape,
    roundedCornerShape: Int = 0,
    contentScale: ContentScale = ContentScale.Crop,
    contentDescription: String = "",
    colorFilter: ColorFilter? = null,
) {
    Image(
        imageVector = ImageVector.vectorResource(id = imageVectorId),
        contentDescription = contentDescription,
        contentScale = contentScale,
        modifier = Modifier
            .padding(
                start = start,
                top = top,
                end = end,
                bottom = bottom,
            )
            .border(width = borderWidth, color = borderColor, borderShape)
            .size(size)
            .clip(RoundedCornerShape(roundedCornerShape)),
        colorFilter = colorFilter
    )

}


/**
 * 加载xml形式的图片
 * @param id Int 图片的id
 * @param size Dp 图片的大小
 * @param start Dp 左边距
 * @param top Dp 上边距
 * @param end Dp 右边距
 * @param bottom Dp 底边距
 * @param roundedCornerShape Dp 圆角的大小
 * @param contentScale ContentScale 裁剪模式
 * @param contentDescription String 描述
 */

@Composable
fun XxImageXml(
    id: Int,
    size: Dp = 100.dp,
    start: Dp = 0.dp,
    top: Dp = 0.dp,
    end: Dp = 0.dp,
    bottom: Dp = 0.dp,
    borderWidth: Dp = 0.dp,
    borderColor: Color = Color.Transparent,
    borderShape: Shape = RectangleShape,
    roundedCornerShape: Int = 0,
    contentScale: ContentScale = ContentScale.Crop,
    contentDescription: String = "",
    colorFilter: ColorFilter? = null
) {

    ResourcesCompat.getDrawable(
        LocalContext.current.resources,
        id, LocalContext.current.theme
    )?.let { drawable ->
        val bitmap = Bitmap.createBitmap(
            drawable.intrinsicWidth, drawable.intrinsicHeight,
            Bitmap.Config.ARGB_8888
        )
        val canvas = Canvas(bitmap)
        drawable.setBounds(0, 0, canvas.width, canvas.height)
        drawable.draw(canvas)

        Image(
            bitmap = bitmap.asImageBitmap(),
            contentDescription = contentDescription,
            contentScale = contentScale,
            modifier = Modifier
                .padding(
                    start = start,
                    top = top,
                    end = end,
                    bottom = bottom,
                )
                .border(width = borderWidth, color = borderColor, borderShape)
                .size(size)
                .clip(RoundedCornerShape(roundedCornerShape)),
            colorFilter = colorFilter
        )
    }
}

/**
 * 加载网络图片
 * @param url String 图片的网络地址
 * @param size Dp 图片的大小
 * @param roundedCornerShape Dp 圆角的大小
 * @param contentScale ContentScale 裁剪模式
 * @param contentDescription String 描述
 * @see  AsyncImage 是由Kotlin 协程 (Instacart) 提供支持的图片加载库 coil  https://github.com/coil-kt/coil#jetpack-compose
 */
//@Composable
//fun XxAsyncImage(
//    url: String,
//    size: Dp = 100.dp,
//    roundedCornerShape: Dp = 0.dp,
//    contentScale: ContentScale = ContentScale.Crop,
//    contentDescription: String = ""
//) {
//    AsyncImage(
//        model = url,
//        contentDescription = contentDescription,
//        contentScale = contentScale,
//        modifier = Modifier
//            .size(size)
//            .clip(RoundedCornerShape(roundedCornerShape))
//    )
//
//}